package com.dtu.XMLExtras;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlSerializer;

import com.dtu.ImagesExtras.ImageAndText;

import android.os.Environment;
import android.util.Log;
import android.util.Xml;

public class ReadXML extends DefaultHandler{

	// ===========================================================
	// Fields
	// ===========================================================
	private ArrayList<String> items =  new ArrayList<String>();
	private String XMLtag = "";
	private String XMLattr = "";
	private String XMLattr2 = "";
	private String XMLpath = "";

	public ReadXML(){
	}
	
	public ReadXML(String tags, String attr, String path){
		XMLtag = tags;
		XMLattr = attr;
		XMLpath = path;
	}

	public void setTag(String tag){
		XMLtag = tag;
	}
	public void setAttr(String attr){
		XMLattr = attr;
	}

	public void setPath(String path){
		XMLpath = path;
	}
	


	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public ArrayList<String> getParsedData() {
		return this.items;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.items = new ArrayList<String>();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/** Gets be called on opening tags like:
	 * <tag>
	 * Can provide attribute(s), when xml was like:
	 * <tag attribute="attributeValue">*/
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (localName.equals(XMLtag)) {
			String tag = atts.getValue(XMLattr);
			items.add(tag);
		}
	}

	public ArrayList<String> getInfo(){
		try {

			/* Create a URL we want to load some xml-data from. */
			URL url = new URL(XMLpath);

			/* Get a SAXParser from the SAXPArserFactory. */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();

			/* Get the XMLReader of the SAXParser we created. */
			XMLReader xr = sp.getXMLReader();
			/* Create a new ContentHandler and apply it to the XML-Reader*/
			ReadXML myXMLHandler = new ReadXML(XMLtag, XMLattr, XMLpath);
			xr.setContentHandler(myXMLHandler);


			/* Parse the xml-data from our URL. */
			xr.parse(new InputSource(url.openStream()));
			/* Parsing has finished. */
			
			
			return myXMLHandler.getParsedData();

		} catch (Exception e) {
		}

		return null;

		/* Our ExampleHandler now provides the parsed data to us. */

	}
}
